<?php
class AttachmentsController extends AppController {
 
	var $name = 'Attachments';
	var $components = array('RequestHandler');
 
	function add() {
		if (!empty($this->data)) {
//                        $this->data['Attachment']['file']['name']=str_replace('-','_',$this->data['Attachment']['file']['name']);
                        $file2=$this->data['Attachment']['file']['name'];
                        $file1=$this->Attachment->find('first', array('order' => array('Attachment.created DESC'), 'fields' => 'id'));
//                         $filetype=str_replace("image/",".",$this->data['Attachment']['file']['type']);
                        if ($this->data['Attachment']['file']['type']=="image/jpeg")
                            $filetype=".jpg";  
//                        else if ($this->data['Attachment']['file']['type']=="image/png")
//                            $filetype=".png";  
//                        else if ($this->data['Attachment']['file']['type']=="image/gif")
//                            $filetype=".gif"; 
//                        else if ($this->data['Attachment']['file']['type']=="audio/mp3")
//                            $filetype=".mp3";  
//                        else if ($this->data['Attachment']['file']['type']=="video/mp4")
//                            $filetype=".mp4"; 
                        else
                            $filetype=".".substr($this->data['Attachment']['file']['type'],-3);
                        $this->data['Attachment']['file']['name']= ($file1['Attachment']['id']+1).$filetype;
                            
			if ($this->Attachment->save($this->data)) {
				$result = '<div id="status">success</div>';
                                $result.= '<div id="filename1">'.$this->data['Attachment']['file']['name'].'</div>';
                                $result.= '<div id="filename2">'.$file2.'</div>';
				$result .= '<div id="message">Successfully Uploaded</div>';
//                                $result.= '<div id="something">'.var_export($this->data,true).'</div>';
			} else {
				$result = "<div id='status'>error</div>";
				$result .= '<div id="message">'. $this->Attachment->validationErrors['file'] .'</div>';
			}
 
			$this->RequestHandler->renderAs($this, 'ajax');
			$this->set('result', $result);
			$this->render('../elements/ajax');
		}
	}
 
}
//get id;
?>